package com.link.base.base.monitor;

import com.link.base.base.appduty.dao.mybatis.mapper.AppDutyMapper;
import com.link.base.base.appduty.model.AppDuty;
import com.link.base.base.appduty.model.AppDutyMenuInter;
import com.link.base.base.appduty.model.AppMenu;
import com.link.base.base.appduty.model.AppMenuSecInter;
import com.link.base.base.appduty.service.AppDutyMenuInterService;
import com.link.base.base.appmenuurls.model.AppMenuUrls;
import com.link.base.base.common.model.Menu;
import com.link.base.base.duty.dao.mybatis.mapper.DutyMapper;
import com.link.base.base.duty.model.Duty;
import com.link.base.base.menupageurls.model.MenuPageUrls;
import com.link.core.util.ObjectChangeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 监控对象内容变化
 *
 * @author qinxg
 * @date 2020-07-08 9:16
 */
@Component
public class MonitorObjectChange {

    @Resource
    private DutyMapper dutyMapper;
    @Resource
    private AppDutyMapper appDutyMapper;
    @Resource
    private AppDutyMenuInterService appDutyMenuInterService;

    // 安全性菜单以及二级菜单层级
    public static String[] MENU_GRADE_1_2 = {"1", "2"};
    // 业务数据的新增/修改
    public static String[] CHANGE_INSERT_UPDATE = {"NEW", "UPDATE"};

    /**
     * 监控业务数据变化导致的职责变化
     *
     * @author qinxg
     * @date 2020/7/8
     * @param obj 业务对象
     * @return
     **/
    public void dutyChange(Object obj,String type) throws Exception {
        // 职责对象
        if (obj instanceof Duty) {
            Long dutyId = ((Duty) obj).getId();
            ObjectChangeUtils.changeEvent(type, "DUTY", dutyId.toString());
        }
        if (obj instanceof AppDuty) {
            Long dutyId = ((AppDuty) obj).getId();
            ObjectChangeUtils.changeEvent(type, "DUTY", dutyId.toString());
        }
        // 菜单对象
        if (obj instanceof Menu) {
            // 判断是安全性菜单还是 二级菜单
            Menu sourceMenu = (Menu) obj;
            Menu menu = new Menu();
            BeanUtils.copyProperties(sourceMenu, menu);
            // 当为安全性菜单的时候
            if ("2".equals(menu.getMenuGrade())) {
                menu.setId(menu.getParentMenuId());
            }
            if (menu.getId() == null) {
                throw new Exception("菜单不存在");
            }
            // 根据菜单id查找职责id
            List<Long> dutyIds = dutyMapper.queryDutyIdsByMenuId(menu);
            for (Long dutyId : dutyIds) {
                ObjectChangeUtils.changeEvent(type, "DUTY", dutyId.toString());
            }
        }
        // CRM/DMS 页面url对象
        if (obj instanceof MenuPageUrls) {
            MenuPageUrls menuPageUrls = (MenuPageUrls) obj;
            Long menuId = menuPageUrls.getMenuId();
            if (menuId == null) {
                throw new Exception("菜单不存在");
            }
            Menu menu = new Menu();
            menu.setId(menuId);
            // 根据菜单id查找职责id
            List<Long> dutyIds = dutyMapper.queryDutyIdsByMenuId(menu);
            for (Long dutyId : dutyIds) {
                ObjectChangeUtils.changeEvent(type, "DUTY", dutyId.toString());
            }
        }
        // APP 页面url对象
        if (obj instanceof AppMenuUrls) {
            AppMenuUrls appMenuUrls = (AppMenuUrls) obj;
            Long menuId = appMenuUrls.getMenuId();
            if (menuId == null) {
                throw new Exception("菜单不存在");
            }
            AppMenu menu = new AppMenu();
            menu.setId(menuId);
            // 根据菜单id查找职责id
            List<String> dutyIds = appDutyMapper.queryAppDutyIdsByMenuId(menu);
            for (String dutyId : dutyIds) {
                ObjectChangeUtils.changeEvent(type, "DUTY", dutyId);
            }
        }
        // APP职责下增加安全性菜单
        if (obj instanceof AppMenuSecInter) {
            AppMenuSecInter appMenuSecInter = (AppMenuSecInter) obj;
            Long parentId = appMenuSecInter.getParentId();
            if (parentId == null) {
                throw new Exception("该安全性不存在父对象");
            }
            AppDutyMenuInter appDutyMenuInter = appDutyMenuInterService.queryById(parentId);
            Long dutyId = appDutyMenuInter.getDutyId();
            ObjectChangeUtils.changeEvent(type, "DUTY", dutyId.toString());
        }
    }
}
